﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FATCATool
{
    public partial class PersonTypeControl : UserControl
    {
        private PersonParty_Type _party;
        private bool _bindingComplete = false;
        FATCASchema.Wrappers.BirthInfo _birthInfo;
        private BindingList<FATCASchema.Wrappers.PersonName> _names;

        public PersonTypeControl()
        {
            InitializeComponent();
        }

        public void Intitalize(PersonParty_Type party)
        {
            _bindingComplete = false;
            _party = party;

            _birthInfo = new FATCASchema.Wrappers.BirthInfo();
            _birthInfo.SetValues(_party.BirthInfo);
            _birthInfo.PropertyChanged += _birthInfo_PropertyChanged;

            _names = new BindingList<FATCASchema.Wrappers.PersonName>();
            if (party.Name != null)
            {
                foreach (var name in _party.Name)
                {
                    var wrapperName = new FATCASchema.Wrappers.PersonName();
                    wrapperName.SetValues(name);
                    _names.Add(wrapperName);
                }
            }

            _names.ListChanged += _names_ListChanged;

            DataBind();
            addressControl.Initialize(_party);
            _bindingComplete = true;
        }

        void _names_ListChanged(object sender, ListChangedEventArgs e)
        {
            _party.Name = _names.Select(r => r.GetFATCANameType()).ToArray();
        }

        void _birthInfo_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            _party.BirthInfo = _birthInfo.GetFATCABirthInto();
        }

        private void DataBind()
        {
            ((DataGridViewComboBoxColumn)grdCountry.Columns["nameDataGridViewTextBoxColumn"]).DataSource = Enum.GetValues(typeof(CountryCode_Type));
            ((DataGridViewComboBoxColumn)grdTIN.Columns["issuedByDataGridViewTextBoxColumn"]).DataSource = Enum.GetValues(typeof(CountryCode_Type));
            ((DataGridViewComboBoxColumn)grdNatCountry.Columns["nameDataGridViewTextBoxColumn1"]).DataSource = Enum.GetValues(typeof(CountryCode_Type));

            if (_party.ResCountryCode != null)
                genericNameBindingSource.DataSource = _party.ResCountryCode.ToList().Select(r => new FATCASchema.Utilities.GenericCountry { Name = r }).ToList();

            if (_party.Nationality != null)
                NationalityBindingSource.DataSource = _party.Nationality.ToList().Select(r => new FATCASchema.Utilities.GenericCountry { Name = r }).ToList();

            if (_party.TIN != null)
                tINTypeBindingSource.DataSource = _party.TIN.ToList();
            else
                tINTypeBindingSource.Clear();

            cmbCountryCode.DataBindings.Clear();
            cmbCountryCode.DataSource = Enum.GetNames(typeof(CountryCode_Type));
            cmbCountryCode.DataBindings.Add("SelectedItem", _birthInfo, "CountryCode");

            dteBirthDate.DataBindings.Clear();
            dteBirthDate.DataBindings.Add("Value", _birthInfo, "BirthDate");

            txtCountryText.DataBindings.Clear();
            txtCountryText.DataBindings.Add("Text", _birthInfo, "CountryName");

            txtBirthCity.DataBindings.Clear();
            txtBirthCity.DataBindings.Add("Text", _birthInfo, "City");

            txtBirthSubEntity.DataBindings.Clear();
            txtBirthSubEntity.DataBindings.Add("Text", _birthInfo, "SubEntity");

            ((DataGridViewComboBoxColumn)grdNames.Columns["nameTypeDataGridViewTextBoxColumn"]).DataSource = FATCA_OECD.GetFriendlyEnumList(typeof(OECDNameType_EnumType));
            ((DataGridViewComboBoxColumn)grdNames.Columns["nameTypeDataGridViewTextBoxColumn"]).ValueMember = "Key";
            ((DataGridViewComboBoxColumn)grdNames.Columns["nameTypeDataGridViewTextBoxColumn"]).DisplayMember = "Value";
            personNameBindingSource.DataSource = null;
            personNameBindingSource.DataSource = _names;

        }

        private void btnAddName_Click(object sender, EventArgs e)
        {
            var name = new FATCASchema.Wrappers.PersonName();

            //show object in details screen
            var cntl = new PersonNameControl();
            cntl.NameItem = name;
            Form form = new Form
            {
                Text = "Name Edit",
                Size = new Size(560, 350)
            };
            form.Controls.Add(cntl);

            if (form.ShowDialog() == DialogResult.OK)
            {
                _names.Add(cntl.NameItem);

                DataBind();
            }
        }

        private void btnEditName_Click(object sender, EventArgs e)
        {
            if (grdNames.SelectedRows.Count != 1 || !(grdNames.SelectedRows[0].DataBoundItem is FATCASchema.Wrappers.PersonName))
                return;

            var name = (FATCASchema.Wrappers.PersonName)grdNames.SelectedRows[0].DataBoundItem;


            var cntl = new PersonNameControl();
            cntl.NameItem = name;
            Form form = new Form
            {
                Text = "Name Edit",
                Size = new Size(560, 350)
            };
            form.Controls.Add(cntl);

            if (form.ShowDialog() == DialogResult.OK)
            {
                _names[_names.IndexOf(cntl.NameItem)] = cntl.NameItem;

                DataBind();
            }
        }

        private void btnDeleteName_Click(object sender, EventArgs e)
        {
            if (grdNames.SelectedRows.Count != 1 || !(grdNames.SelectedRows[0].DataBoundItem is FATCASchema.Wrappers.PersonName))
                return;

            var name = (FATCASchema.Wrappers.PersonName)grdNames.SelectedRows[0].DataBoundItem;

            _names.Remove(name);

            DataBind();
        }

    }
}
